package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.umeng.analytics.MobclickAgent;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.AnchorActivity;
import com.xgkj.diyiketang.activity.BangDanActivity;
import com.xgkj.diyiketang.activity.SearchActivity;
import com.xgkj.diyiketang.activity.SearchAnchorActivity;
import com.xgkj.diyiketang.activity.data.PaiHangAdatper;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AnchorListTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class Fragment_PaiHang extends BaseFragment implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
	private Context mContext;
	private PullToRefreshListView mListView;
	private PaiHangAdatper mAdapter;
	private int currentPage = 1;
	private int size = 10;
	private int paihangbangTaskId = -1;
	private ImageView sousuo;

	@Override
	protected int getContentViewId() {
		return 0;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_paihang,null);
		mContext = getActivity();
		initView(view);
		return view;
	}
	private void initView(View view){
		sousuo = (ImageView) view.findViewById(R.id.paihang_search);
		sousuo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				startActivity(new Intent(mContext, SearchAnchorActivity.class));
			}
		});
		mListView = (PullToRefreshListView)view.findViewById(R.id.paihang_list);
		mListView.init(mContext,true);
		mAdapter = new PaiHangAdatper(mContext);
		mListView.setAdapter(mAdapter,true);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//PalLog.e("位置",position+"");
				if (mListView.getHeaderViewsCount()>0){
					position--;
				}
				if (position < 0 || position>=mAdapter.getCount()) return;
				EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position);
				startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
//                String uid = info.getUserId();
//
//                Intent intent = new Intent(mContext,AnchorActivity.class);
//                intent.putExtra("USERID",uid);
//
//                startActivity(intent);
			}
		});
		mListView.setOnRefreshListener(this);
		mListView.setOnLoadDataListener(this);
		mListView.doRefresh();
	}



	private void getPaiHaiBang() {
		if(paihangbangTaskId != -1){
			return;
		}
		if(currentPage == 0){
			mListView.setNoNextPage();
			return;
		}

		AnchorListTask task = new AnchorListTask(size, currentPage,"", "","","1",null);
		task.addListener(new AnchorListener(mContext));
		paihangbangTaskId = getEatShowApp().getTaskManager().addTask(task);
	}

	class AnchorListener extends ShowToastTaskEventListener{
		public AnchorListener(Context context) {
			super(context);
		}
		@Override
		public void handleTaskCompleted(int taskId, int result, Object data) {
			super.handleTaskCompleted(taskId, result, data);
			if(taskId == paihangbangTaskId){
				paihangbangTaskId = -1;
				if(currentPage == 1){
					mAdapter.clearData();
					mListView.onRefreshComplete();
					mListView.enableLoadMore();
				}else{
					mListView.onLoadDataComplete(true);
				}
				List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
				if(data != null){
					fansList = (List<EatShowUserInfo>) data;
				}

				if(fansList != null && fansList.size()>0){
					mAdapter.updateData(fansList);
				}

				currentPage = result;

			}
		}

		@Override
		public void handleTaskError(int taskId, int errorCode, String errorMsg) {
			super.handleTaskError(taskId, errorCode, errorMsg);
			if(taskId == paihangbangTaskId){
				paihangbangTaskId = -1;
				if(currentPage == 1){
					mListView.onRefreshComplete();
				}else{
					mListView.onLoadDataComplete(false);
				}
			}
		}

		@Override
		public void handleTaskMessage(int taskId, Object data) {
			super.handleTaskMessage(taskId, data);
		}

	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden){
			//mListView.invalidate();
			if(mAdapter != null && mAdapter.getCount() == 0){
				mListView.doRefresh();
			}

		}
	}
	@Override
	public void onPullRefresh() {

	}

	@Override
	public void onLoadMore() {
		getPaiHaiBang();
	}

	@Override
	public void onRefresh() {
		currentPage=1;
		getPaiHaiBang();

	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("PaiHangFragment");
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("PaiHangFragment");
	}
}
